﻿namespace CookieTerm
{
    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.ComponentModel;

    public static class ColorExtension
    {
        public static Color ToColor(this XmlColor xmlColor)
        {
            return Color.FromArgb(xmlColor.R, xmlColor.G, xmlColor.B);
        }
    }

    class ColorBox : Control, INotifyPropertyChanged
    {
        XmlColor color;

        ColorDialog dialog = new ColorDialog();

        public event PropertyChangedEventHandler PropertyChanged;

        [Browsable(false)]
        public XmlColor Color 
        {
            get
            {
                return this.color;
            }

            set
            {
                this.color = value;
                Invalidate();
            }
        }

        public Color BorderColor { get; set; }

        protected override void OnDoubleClick(EventArgs e)
        {
            this.dialog.Color = Color.ToColor();

            if (this.dialog.ShowDialog() == DialogResult.OK)
            {
                BackColor = Color.ToColor();
                Color = XmlColor.FromRgb(this.dialog.Color.ToArgb());
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Color"));
                }
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            BackColor = Color.ToColor();

            Rectangle r = new Rectangle(1, 1, Bounds.Width - 2, Bounds.Height - 2);
            e.Graphics.DrawRectangle(new Pen(new SolidBrush(BorderColor),2), r);
        }
    }
}
